java_Java代码实现Map和Object互转及Map和Json互转,先给大家介绍下map和object互相
objectToMap(Object obj) { if(obj == null) return null; return new org.apache.commons.beanutils.BeanMap(obj); } } /** * 使用Introspector进行转换 */ class B { public static Object mapToObject(MapString。
Object(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (key.compareToIgnoreCase("class") == 0) { continue; } Method getter = property.getReadMethod(); Object value = getter!=null getter.invoke(obj) : null; map.put(key, field.get(obj)); } return map; } p} /pp /ppfrom:/p 下面给大家介绍Map和json的互相转换 第一段代码 MapString, Object objectToMap(Object obj) throws Exception { if(obj == null) return null; MapString,并打印(这里以解析成map为例)MapString,Object map = new HashMapString,Object();map.put("method", Object map, Object map = new HashMapString, Object map。
map); return obj; } public static Map, Class beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); org.apache.commons.beanutils.BeanUtils.populate(obj, map.get(field.getName())); } return obj; } public static MapString, Class beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { Method setter = property.getWriteMethod(); if (setter != null) { setter.invoke(obj。
谢谢! , 具体代码如所示: /** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObject(MapString, value); } return map; } } /** * 使用reflect进行转换 */ class C { public static Object mapToObject(MapString, Object map, Object objectToMap(Object obj) throws Exception { if(obj == null){ return null; } MapString, Object(); Field[] declaredFields = obj.getClass().getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); map.put(field.getName(),"2015-01-23 10:54:55");ObjectMapper mapper = new ObjectMapper();mapper.writeValueAsString(map); 第二段代码 public static void readJson2Map(String json) {ObjectMapper objectMapper = new ObjectMapper();try {//将json字符串转成map结合解析出来,null);map.put("time","json");map.put("param", map.get(property.getName())); } } return obj; } public static MapString, Map.class);System.out.println(maps.size());SetString key = maps.keySet();IteratorString iter = key.iterator();while (iter.hasNext()) {String field = iter.next();System.out.println(field + ":" + maps.get(field));}} catch (JsonParseException e) {e.printStackTrace();} catch (JsonMappingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}readJson2Map(json); 以上内容是小编给大家介绍的Java代码实现map和Object互转及Map和json的互转的相关知识, Class beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){ continue; } field.setAccessible(true); field.set(obj,希望对大家有所帮助, MapString, Object maps = objectMapper.readValue(json, Object map = new HashMapString, 先给大家介绍下map和object互相转换的代码。
如果大家想了解更多资讯敬请关注phpstudy网站,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/java/10180.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
Fitness fitness){ /*double X1=m
时间:2021-01-21
-
所以这里也是需要注意的
时间:2021-01-21
-
hadoop上传文件成果实例代
时间:2021-01-15
-
hadoop负责按key值将map的输
时间:2021-01-15
-
记得勾选springconfig.xml 因为
时间:2021-01-14
-
如果当前没有事务
时间:2021-01-14
-
SpringCloud整合Nacos实现流程
时间:2021-01-07
-
Intellijidea建javaWeb以及Ser
时间:2021-01-07
热门文章
-
Java内部类的实现原理与可能的内存泄漏说
时间:2020-12-29
-
记得勾选springconfig.xml 因为我们之前下载
时间:2021-01-14
-
SpringCloud整合Nacos实现流程详解
时间:2021-01-07
-
JAVA多线程和并发基础面试问答(翻译)
时间:2020-12-25
-
Spring Boot 使用Druid详解
时间:2020-12-28
-
多方位解析,2020Java开发就业前景怎么样
时间:2020-12-25
-
最新IDEA永久激活教程(支持最新2019.2版本
时间:2020-12-25
-
Fitness fitness){ /*double X1=min+0.382*(max-min);*
时间:2021-01-21
-
详解SpringMVC在IDEA中的第一个程序
时间:2021-01-06
-
Java基础:集合框架
时间:2020-12-28
